package com.firefighting.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.firefighting.entity.Building;
import com.firefighting.entity.Contract;
import com.firefighting.entity.Enclosure;
import com.firefighting.entity.Task;
import com.firefighting.entity.User;
import com.firefighting.entity.UserBasicInfo;
import com.firefighting.service.BuildingService;
import com.firefighting.service.ContractService;
import com.firefighting.service.EnclosureService;
import com.firefighting.service.LocationService;
import com.firefighting.service.TaskService;
import com.firefighting.service.UserBasicInfoService;
import com.firefighting.util.WriterUtil;

@Controller
@RequestMapping("location")
public class LocationController {

	
	@Autowired
	private LocationService locationService;
	@Autowired
	private UserBasicInfoService userbasic;
	@Autowired
	private ContractService contractservice;
	
	/**
	 * @function 根据企业id查询该企业维保工程师的位置情况
	 * @author Hanjd
	 * 
	 * @return
	 * 
	 * 
	 */
	@RequestMapping("getAllLocation")
	private String getBuildingInfo(HttpServletRequest request, Model model)throws JsonGenerationException, JsonMappingException, IOException
	{
		List<UserBasicInfo> nameList = new ArrayList<UserBasicInfo>(); 
		List<Contract> projectList = new ArrayList<Contract>();
		User user = (User) request.getSession().getAttribute("currentUser");
		String username = user.getUsername();
		int enterpriseId = userbasic.queryEnterpriseIdByUsername(username);
		List<HashMap<String, Object>> locatonList = new ArrayList<HashMap<String,Object>>();
		ObjectMapper objectMapper = new ObjectMapper();
		
		locatonList = locationService.queryLocationEnterpriseId(enterpriseId);
		
		nameList = userbasic.getRoleInfoByEnter(enterpriseId,3);
		
		projectList = contractservice.getContractByEnterpriseId1(enterpriseId);
		
		model.addAttribute("nameList",nameList);
		model.addAttribute("projectList",projectList);
		model.addAttribute("locatonList", objectMapper.writeValueAsString(locatonList));
		return "person/position";
	
	}
	/**
	 * @function 根据企业id,人员姓名，起始时间查询该企业维保工程师的位置情况
	 * @author Hanjd
	 * */
	@RequestMapping("searchLocation")
	public String serchLocation(HttpServletRequest request,Model model) throws JsonGenerationException, JsonMappingException, IOException{
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		String name = "-1";
		int contractName = -1;
		Date firstDay = new Date();
		Date lastDay = new Date();
		String splitTime[] = new String[2];
		JSONObject result=new JSONObject();
		List<UserBasicInfo> nameList = new ArrayList<UserBasicInfo>(); 
		List<Contract> projectList = new ArrayList<Contract>();
		User user = (User) request.getSession().getAttribute("currentUser");
		String username = user.getUsername();
		int enterpriseId = userbasic.queryEnterpriseIdByUsername(username);
		name = request.getParameter("peopleName") == null?"-1":request.getParameter("peopleName");
		contractName = request.getParameter("projectName")== null?-1:Integer.valueOf(request.getParameter("projectName"));
		String reservation = request.getParameter("reservation");
		GetDate getDate = new GetDate();
		if(reservation != null){
			splitTime = getDate.dateSplit(reservation);
		}else{
			splitTime[0] = format.format(firstDay);;
			splitTime[1] = format.format(lastDay);;
		}
		   
		if (name.equals("-1")) {
			name="";
		}
		if (contractName==-1) {
			contractName=0;
		}
			

			try{
				firstDay = sf.parse(splitTime[0]);
				//Date lastDay2 = sf.parse(splitTime[1]);
				Date d2=sf.parse(splitTime[1]);
				int dayMis=1000*60*60*24;//一天的毫秒
				long curMillisecond=d2.getTime();//当天的毫秒  
				long resultMis=curMillisecond+(dayMis-1); //当天最后一秒  

				Date resultDate=new Date(resultMis);  
				lastDay = format.parse(format.format(resultDate));
			}catch(ParseException e){
				e.printStackTrace();
			}

			
		
		List<HashMap<String, Object>> locatonList = new ArrayList<HashMap<String,Object>>();
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			locatonList = locationService.queryLocationSearch(enterpriseId, name,contractName, firstDay, lastDay);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("flag", false);
			return JSONObject.toJSONString(map);
		}
		nameList = userbasic.getRoleInfoByEnter(enterpriseId,3);
		
		projectList = contractservice.getContractByEnterpriseId1(enterpriseId);
		model.addAttribute("name",name);
		model.addAttribute("contractName",contractName);
		model.addAttribute("nameList",nameList);
		model.addAttribute("projectList",projectList);
		model.addAttribute("locatonList", objectMapper.writeValueAsString(locatonList));
		return "person/position";
	}
	
}
